package com.my.study.io;

import lombok.extern.slf4j.Slf4j;

import java.io.*;

/**
 * @author Carlos
 * @version 1.0
 * @Description 文件拷贝
 * @date 2021/6/27 8:52
 **/
@Slf4j(topic = "c.BufferCopyTest")
public class BufferCopyTest {

    private static final String SRC_PATH = "D:/file/ok.txt";

    private static final String DEST_PATH = "D:/file/ok1.txt";

    public static void main(String[] args) {
        String line;

        try(BufferedReader reader = new BufferedReader(new FileReader(SRC_PATH));
            BufferedWriter writer = new BufferedWriter(new FileWriter(DEST_PATH))) {

            while ((line = reader.readLine()) != null) {
                writer.write(line);
                writer.newLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private static void test01() {
        BufferedReader reader = null;
        BufferedWriter writer = null;
        String line;
        try {
            reader = new BufferedReader(new FileReader(SRC_PATH));
            writer = new BufferedWriter(new FileWriter(DEST_PATH));

            // 读取并写入
            while ((line = reader.readLine()) != null) {
                writer.write(line);
                writer.newLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
